﻿using System;
using System.Windows.Forms;
using System.Xml;

using Core;
using ClassLibrary;

namespace TestProject
{
    public partial class NewGenPhenLink : Form
    {
        private Model model;
        private PhenotypeTable phen_t=null;
        private GeneTable gen_t=null;
        private string path=null,phenname;
        private decimal amount;
        private bool b = false;
        public NewGenPhenLink(Model m)
        {
            InitializeComponent();
            model = m;
        }
        public NewGenPhenLink(Model m, string n,string s, decimal d)
        {
            InitializeComponent();
            model = m;
            path = s;
            amount = d;
            b = true;
            phenname = n;
        }
        private void create_Click(object sender, EventArgs e)
        {
            if((gen_t_box.SelectedItem==null)&&(gen_t_box.Text==""))
            {
                MessageBox.Show("Please, choose gene table.");
                return;
            }
            if ((phen_t_box.SelectedItem==null)&&(phen_t_box.Text==""))
            {
                MessageBox.Show("Please, choose phenotype table.");
                return;
            }
            if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Links\\GenomePhenotypeLinks\\" + name_box.Text + ".xml"))
            {
                MessageBox.Show("A genome phenotype link with this name already exists. Please, choose another name.");
                return;
            }
            if (!b)
            {
                foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
                {
                    if ((string)phen_t_box.SelectedItem == t.name)
                    {
                        phen_t = t;
                        break;
                    }
                }
                path = phen_t.path;
            }
            foreach (GeneTable t in model.model_gene_tables.Values)
            {
                if ((string)gen_t_box.SelectedItem == t.name)
                {
                    gen_t = t;
                    break;
                }
            }
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Links\\GenomePhenotypeLinks\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("link");
            writer.WriteAttributeString("Type", "genome_and_phenotype");
            writer.WriteAttributeString("Name", name_box.Text);            
            writer.WriteAttributeString("PathToGeneTable", gen_t.path);
            writer.WriteAttributeString("PathToPhenotypeTable", path);
            writer.WriteAttributeString("CharacteristicsAmount", amount.ToString());
            writer.WriteStartElement("characteristics");
            if (b)
            {
                for (int i = 0; i < amount; i++)
                {
                    writer.WriteStartElement("characteristic");
                    writer.WriteAttributeString("Id", i.ToString());
                    writer.WriteAttributeString("Name", "Char_" + i.ToString());
                    writer.WriteStartElement("values");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
            else
            {
                foreach (CharacteristicDescription charac in phen_t.characteristic_table.Values)
                {
                    writer.WriteStartElement("characteristic");
                    writer.WriteAttributeString("Id", charac.id.ToString());
                    writer.WriteAttributeString("Name", charac.name);
                    writer.WriteStartElement("values");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
            XmlElement el;
            el = doc.CreateElement("link");
            el.SetAttribute("Path", "file_input\\" + model.model_name + "\\Links\\GenomePhenotypeLinks\\" + name_box.Text + ".xml");
            doc.Increment(doc.GetElementsByTagName("links")[0].Attributes[1]);
            doc.Increment(doc.DocumentElement.Attributes[2]);
            doc.GetElementsByTagName("genome_and_phenotype_links")[0].AppendChild(el);
            doc.Save(model.model_path);
            Functions.add_node_func(new GenomePhenotypeLink("file_input\\" + model.model_name + "\\Links\\GenomePhenotypeLinks\\" + name_box.Text + ".xml"), name_box.Text, "genphl");
            this.Close();
        }

        private void canc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void NewGenPhenLink_Load(object sender, EventArgs e)
        {
            if (b)
            {
                phen_t_box.Text = phenname;
                phen_t_box.Enabled = false;
            }
            else
            {
                foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
                {
                    phen_t_box.Items.Add(t.name);
                }
            }
                foreach (GeneTable t in model.model_gene_tables.Values)
                {
                    gen_t_box.Items.Add(t.name);
                }
        }

    }
}
